//点击头部跳转首页
$(".headerUl li").each(function(i,n){
    $(n).click(function(){
        window.location.href="../index.html"
    })
})


var heji=0;
var hejiPrice=0;
    $.ajax({
        type: "get",
        url: "../php/shopping.php",
        data:{"usertel":getCookie("logintel")},
        dataType: "json",
        success: function (list) {
            // $(".spcontent").empty();
            var html="";
            $.each(list,function(i,n){
             var{
                price,
                goodscolor,
                dingzhi,
                num,
                goodsname,
                smallimg,
                goodsid,
             }=n;
             html+=`
                    <ul class="sptr w cl">
                    <li class="sptrli1">
                        <img src="../images/${smallimg}" alt="">
                        <div class="sptr1 fl">
                            <div>${goodsname}</div>
                            <p>礼物颜色:<span>${goodscolor}</span></p>
                            <p>个人定制:<span>${dingzhi}</span></p>
                        </div>
                    </li>
                    <li class="iconfont icon-qian" class="price">${price}</li>
                    <li class="sptr3" goodscolor=${goodscolor} goodsid=${goodsid} dingzhi=${dingzhi}>
                        <div class="sptr3div cl">
                            <span class="reduce">-</span>
                            <span class="count">${num}</span>
                            <span class="add">+</span>
                        </div>
                    </li>
                    <li class="iconfont icon-qian total" >${price*num}</li>
                    <li class="del">删除</li>
                </ul>
             
             `
            })
            $(".spcontent").css("height","");
            $(".spcontent").append(html);
            //行总价
            
           
            //总合计数
            $.each($(".count"),function(i,n){
                heji+=$(n).html()*1;
            })
            $(".heji").html(heji);
            //总价格
            $.each($(".total"),function(i,n){
                hejiPrice+=$(n).html()*1;
            })
            $(".hejiPrice").html(hejiPrice);
            
        }
    });


// 


//加减删
$(document).on("click",".add",function(i,n){
    var count=$(this).prev().html();
    count++
    $(this).prev().html(count);
    //单合计
    var price= $(this).parent().parent().prev().html();
    var total=price*($(this).prev().html());
    $(this).parent().parent().next().html(total); 
    //总合计数
    heji++
    $(".heji").html(heji);
    //总价格
    hejiPrice=0;
    $.each($(".total"),function(i,n){
        
        hejiPrice+=$(n).html()*1;
    })
    $(".hejiPrice").html(hejiPrice);
    var that=this;
    showData(that,count);
    
})
//减
$(document).on("click",".reduce",function(i,n){
    var count=$(this).next().html();
    count--
    if(count<=0){
        count=1;
        return false;
    }
    $(this).next().html(count);
    var price= $(this).parent().parent().prev().html();
    var total=price*($(this).next().html());
    $(this).parent().parent().next().html(total);
    //总合计数    
      heji--;
    $(".heji").html(heji); 
    //总价格
    hejiPrice=0;
    $.each($(".total"),function(i,n){
        hejiPrice+=$(n).html()*1;
    })
    $(".hejiPrice").html(hejiPrice);
    var that=this;
    showData(that,count);
})
//删
$(document).on("click",".del",function(){
    $(this).parent().remove();
    //总合计数
    heji=0;
    $.each($(".count"),function(i,n){
        heji+=$(n).html()*1;
    })
    $(".heji").html(heji);
    //总价格
    hejiPrice=0;
    $.each($(".total"),function(i,n){
        hejiPrice+=$(n).html()*1;
    })
    $(".hejiPrice").html(hejiPrice);
    var goodsid=$(this).siblings(".sptr3").attr("goodsid");
    var goodscolor=$(this).siblings(".sptr3").attr("goodscolor");
    var dingzhi=$(this).siblings(".sptr3").attr("dingzhi");
    var usertel=getCookie("logintel");
    $.ajax({
        type: "get",
        url: "../php/del.php",
        data:  {"usertel":usertel,"goodscolor":goodscolor,"goodsid":goodsid,"dingzhi":dingzhi},
        success: function (result) {
            if(result){
                // alert("删除成功");
                var usertel=getCookie("logintel");
                if(usertel){
                    $.ajax({
                        type: "get",
                        url: "../php/num.php",
                        data: {"usertel":usertel},
                        dataType: "json",
                        success: function (item) {
                            if(!item[0]){
                                $(".spfooter").css("display","none");
                                $(".spheader").css("display","none");
                                $(".spcontent").css("background","url(../images/shopping.jpg) no-repeat");
                                $(".gobox").css("display","block");
                            }
                           
                        }
                    });
                }
                    
            }else{
                alert("失败");
            }
        }
    });
   
})
$(".jixu").click(function(){
    window.location.href="../index.html";
})

function showData(that,count){
    console.log(count);
    var goodsid=$(that).parent().parent().attr("goodsid");
    var goodscolor=$(that).parent().parent().attr("goodscolor");
    var dingzhi=$(that).parent().parent().attr("dingzhi");
    $.ajax({
        type: "get",
        url: "../php/changeshopping.php",
        data: {"count":count,"goodscolor":goodscolor,"goodsid":goodsid,"dingzhi":dingzhi},
        success: function (count) {
            if(count<=0){
                alert("网络异常")
            }else{
                // alert("成功")
            }
        }
    });
}
//登录以后小人里面放用户电话号码
$(".icon-gerenxinxi").hover(
    function(){
        if(getCookie("logintel")){
            $(".xinxiboxLi").html(getCookie("logintel"))
        }
    }
)
//计数
var usertel=getCookie("logintel");
if(usertel){
    $.ajax({
        type: "get",
        url: "../php/num.php",
        data: {"usertel":usertel},
        dataType: "json",
        success: function (item) {
            if(item[0]>0){
                $(".spfooter").css("display","block");
                $(".spheader").css("display","block");
                $(".spcontent").css("background","none");
                $(".gobox").css("display","none");
            }
           
        }
    });
}
//gobox
$(".gobox").click(function(){
    window.location.href="../index.html";
})
//点击结算跳转到结算页面
$("#pay").click(function(){
    window.location.href="../html/jiesuan.html";
})

